<template>
  <view>
      <z-paging ref="paging" v-model="list" @query="queryList" use-page-scroll :hide-empty-view="hideEmptyView"
                :refresher-enabled="false" @contentHeightChanged="contentHeightChanged" :auto="false"
                :auto-clean-list-when-reload="false">
        <view class="flex flex-sub flex-direction">
          <HospitalPopupSelect @on-change="reload"  v-model:hospital="andParams.hospital" mode="select"></HospitalPopupSelect>
        </view>
        <view class="item" v-for="(item,index) in list" :key="index" @click="handleCreate(item)">
          <ScheduleItem :schedule-item="item"></ScheduleItem>
        </view>
<!--        <uni-card v-for="(item,index) in list" :key="index" @click="handleCreate(item)">-->
<!--          <view class="flex justify-between">-->
<!--            <view class="cu-avatar radius xl"-->
<!--                  style="background-image:url(https://ossweb-img.qq.com/images/lol/web201310/skin/big81005.jpg);">-->
<!--            </view>-->
<!--            <view class="flex flex-sub margin-left flex-direction">-->
<!--              <view class="flex justify-between">-->
<!--                <view class="text-bold text-xl flex align-center">-->
<!--                  <view>{{ item.nurseName }}</view>-->
<!--                  <view class="cu-tag margin-left-xs light">主治</view>-->
<!--                </view>-->
<!--                <view v-if="item.isAppointment===0">-->
<!--                  <button class="cu-btn round sm bg-green" @click="handleCreate(item)">预约</button>-->
<!--                </view>-->
<!--                <view v-if="item.isAppointment===1">-->
<!--                  <button class="cu-btn round sm ">已预约</button>-->
<!--                </view>-->
<!--              </view>-->
<!--              <view>已预约：{{ item.appointmentNum }}</view>-->
<!--              <view>时间：{{ changeTime(item.start) }} - {{ changeTime(item.end) }}</view>-->
<!--            </view>-->
<!--          </view>-->
<!--        </uni-card>-->
      </z-paging>

  </view>
</template>

<script>

import myMoment from "@/utils/common/myMoment";
import api from "@/api";
import UniCard from "@/uni_modules/uni-card/components/uni-card/uni-card";
import ScheduleItem from "@/components/common/item/ScheduleItem";
import HospitalPopupSelect from "@/components/common/select/HospitalPopupSelect";

export default {
  components: {HospitalPopupSelect, ScheduleItem, UniCard},
  data() {
    return {
      //v-model绑定的这个变量不要在分页请求结束中自己赋值！！！
      list: [],
      andParams:{
        hospital:'',
      },
      orParams:{},
      orderParams:{
        start:'1'
      },
      height: 0,
      hideEmptyView: true,
      completeFunc: null
    }
  },
  props: {
    //当前组件的index，也就是当前组件是swiper中的第几个
    tabIndex: {
      type: Number,
      default: function () {
        return 0
      }
    },
    //当前swiper切换到第几个index
    currentIndex: {
      type: Number,
      default: function () {
        return 0
      }
    }
  },
  watch: {
    currentIndex: {
      handler(newVal) {
        if (newVal === this.tabIndex) {
          //懒加载，当滑动到当前的item时，才去加载
          setTimeout(() => {
            this.$refs.paging.reload();
          }, 100);
        }
      },
      immediate: true
    }
  },
  methods: {
    handleCreate(row) {
      api.patient.info({}).then(response => {
        if (response && response.data.code === 0) {
          if (response.data.data === null) {
            //跳转认证
            uni.redirectTo({
              url: '/pages/patient/info/create',
              success: () => {
                uni.showToast({
                  icon: 'error',
                  title: '请先完善身份',
                })
              }
            })
          } else {
            let params = {
              schedule: row,
              patientInfo: response.data.data
            }
            uni.navigateTo({
              url: '/pages/patient/appointment/create?params=' + encodeURIComponent(JSON.stringify(params))
            })
          }
        }
      })
    },
    queryList(pageNo, pageSize) {
      //组件加载时会自动触发此方法，因此默认页面加载时会自动触发，无需手动调用
      //这里的pageNo和pageSize会自动计算好，直接传给服务器即可
      //模拟请求服务器获取分页数据，请替换成自己的网络请求
      let start = myMoment().add(this.tabIndex - 1, 'd').startOf('day').format("yyyy-MM-DD HH:mm:ss")
      let end = myMoment().add(this.tabIndex - 1, 'd').endOf('day').format("yyyy-MM-DD HH:mm:ss")
      if (this.tabIndex === 0) {
        start = myMoment().format("yyyy-MM-DD HH:mm:ss")
        end = myMoment().add(1, 'y').format("yyyy-MM-DD HH:mm:ss")
      }else if(this.tabIndex===1){
        start = myMoment().format("yyyy-MM-DD HH:mm:ss")
      }
      api.schedule.patientBetween({
        start: start,
        end: end,
        pageNo: pageNo,
        pageSize: pageSize,
        andParams:JSON.stringify(this.andParams),
        orParams:JSON.stringify(this.orParams),
        orderParams:JSON.stringify(this.orderParams)
      }).then(res => {
        if (res && res.data.code === 0) {
          let tmp = res.data.data.records
          this.$refs.paging.complete(tmp);
        }
        this.hideEmptyView = false;
        if (this.completeFunc) {
          this.completeFunc();
        }
      }).catch(res => {
        //如果请求失败写this.$refs.paging.complete(false);
        //注意，每次都需要在catch中写这句话很麻烦，z-paging提供了方案可以全局统一处理
        //在底层的网络请求抛出异常时，写uni.$emit('z-paging-error-emit');即可
        this.$refs.paging.complete(false);
        //请求结束，调用父组件的下拉刷新结束回调函数，使得父组件中的z-paging下拉刷新结束
        if (this.completeFunc) {
          this.completeFunc();
        }
      })
    },
    //页面通知当前子组件刷新列表
    doReload(completeFunc) {
      this.andParams.hospital=''
      //先把父组件下拉刷新的回调函数存起来
      this.completeFunc = completeFunc;
      //调用z-paging的reload方法
      this.$refs.paging.reload();
    },
    reload(){
      this.$refs.paging.reload();
      this.$nextTick()
    },
    //当列表高度改变时，通知页面的swiper同步更改高度
    contentHeightChanged(height) {
      const finalHeight = this.list.length ? height : 0;
      //限制内容最小高度为屏幕可见高度减z-tabs高度。注意，页面中有使用slot="top"插入的view，则此处的minHeight还应该减去slot="top"插入的view的高度
      const minHeight = uni.getSystemInfoSync().windowHeight - uni.upx2px(80);
      this.$emit('heightChanged', Math.max(finalHeight, minHeight));
    },
    //页面通知当前子组件加载更多数据
    doLoadMore() {
      this.$refs.paging.doLoadMore();
    },
    //页面通知当前子组件清除数据
    doClear() {
      this.$refs.paging.clear();
      this.hideEmptyView = true;
    }
  }
}
</script>

<style scoped>

</style>